/// <reference path="../../../../../phaser.d.ts" />

import {
  BaseScene
} from "../object/baseScene";
import {
  EnemyPlanes
} from "../object/enemyPlanes";
import {
  PlayerPlane
} from "../object/playerPlane";
import {
  PropSupply
} from "../object/propSupply";

export class GameScene extends BaseScene {
  constructor() {
    super({
      key: 'GameScene'
    })
  }
  // 场景
  bg = null // 游戏背景 
  scoreText = null // 计分牌
  startBtn = null // 开始游戏按钮 
  audioBtn = null // 音量按钮
  // 对象类
  player = null // 玩家飞机
  enemyPlanes = null // 敌机组
  props = null // 道具组
  // 定时器
  gameSpeed = null // 游戏速度控制器
  // 音乐与音效
  audio = false
  sounds = {}
  // 游戏参数
  gameOver = false // 游戏状态
  speed = 0.4 // 全局游戏速度
  score = 0 // 得分

  init() {
    this.sounds.bgm = this.sound.add('bgm', {
      loop: true,
      volume: 0.05,
    })
    this.sounds.gameover = this.sound.add('gameover', {
      volume: 0.2
    })
    this.sounds.getBuff = this.sound.add('getBuff',{volume:0.3})
    // 音量控件
    this.audioBtn = this.add.image(this.gameWidth - 20, 20, 'audio',1)
    this.audioBtn.setScale(0.5, 0.5).setDepth(100).setInteractive()
    this.audioBtn.on('pointerdown',()=>{
      if (this.audio){
        this.sounds.bgm.stop()
        this.audioBtn.setFrame(1)
      }else{
        this.sounds.bgm.play()
        this.audioBtn.setFrame(0)
      }
      this.audio = !this.audio
    })

  }
  // 提速 加快游戏节奏
  increaseSpeed() {
    if (this.gameOver) return
    this.speed = this.speed >= 3 ? 3 : this.speed + 0.1
    this.enemyPlanes.refresh()
    this.propSupply.refresh()
    console.log('speed:', this.speed)
  }
  // 加分
  extraScore(val) {
    this.score += val
    this.scoreText.setText('得分:' + this.score)
  }
  // 游戏失败
  playerFail() {
    this.gameOver = true
    this.sounds.bgm.stop()
    if (this.audio) this.sounds.gameover.play()
    this.player.pause()
    this.enemyPlanes.pause()
    this.propSupply.pause()
    this.time.removeEvent(this.gameSpeed)
    this.startBtn = this.add.image(this.gameCenterX, this.gameCenterY + 100, 'startBtn')
    this.startBtn.setScale(0.5, 0.5)
    this.startBtn.setInteractive()
    this.startBtn.on('pointerdown', () => {
      this.startBtn.destroy()
      this.reset()
    }, this)
  }
  // 重试 开始游戏
  reset() {
    this.speed = 0.4
    this.extraScore(-this.score)
    this.player.reset()
    this.enemyPlanes.reset()
    this.propSupply.reset()
    this.gameOver = false

    this.tweens.killAll()
    // 每5秒提升难度
    this.gameSpeed = this.time.addEvent({
      delay: 5000,
      loop: true,
      callback: () => {
        if (this.gameOver) return
        this.increaseSpeed()
      }
    })
    this.sounds.gameover.stop()
    if (this.audio) this.sounds.bgm.play()
  }
  // 显示信息
  show(text, x, y) {
    let showText = this.add.text(x, y, text, {
      color: '#000',
      fontSize: 20,
      fontFamily: '楷体',
      alpha: 0
    }).setOrigin(0.5, 0.5).setDepth(100)
    this.tweens.add({
      targets: showText,
      duration: 500,
      alpha: 1,
      y: showText.y - 20,
      onComplete: () => {
        this.tweens.add({
          targets: showText,
          duration: 500,
          alpha: 0,
          y: showText.y - 20,
        })
      }
    })
  }
  create() {
    this.bg = this.add.tileSprite(this.gameCenterX, this.gameCenterY, this.gameCenterX * 2, this.gameCenterY * 2, 'gameBG')
    this.enemyPlanes = new EnemyPlanes(this) // 玩家类
    this.propSupply = new PropSupply(this) // 道具生产线
    this.player = new PlayerPlane(this) // 敌机生产线

    this.scoreText = this.add.text(20, 20, '得分:' + this.score, {
      fontSize: 18,
      color: '#000',
      fontFamily: '楷体',
    })
    this.scoreText.setDepth(100)
    this.reset()

  }
  update() {
    if (this.gameOver) {

    } else {
      this.bg.tilePositionY -= this.speed
      this.enemyPlanes.update()
      this.propSupply.update()
      this.player.update()
    }
  }
}
